
package com.insigma.business.qrcode.soldier._provider.impl;


import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.SoldierPetitionInfoDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.entity.SoldierBasicInfoDO;
import com.insigma.business.qrcode.soldier.entity.SoldierPetitionInfoDO;
import com.insigma.business.qrcode.soldier.request.*;
import com.insigma.business.qrcode.soldier.service.SoldierBasicInfoService;
import com.insigma.business.qrcode.soldier._provider.SoldierPetitionInfoProvider;
import com.insigma.business.qrcode.soldier.service.SoldierPetitionInfoService;
import com.insigma.business.qrcode.common.converter.SoldierPetitionInfoProviderConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
@Component("soldierPetitionInfoProvider")
public class SoldierPetitionInfoProviderImpl extends BaseProviderImpl implements SoldierPetitionInfoProvider {

    @Autowired
    private SoldierPetitionInfoService soldierPetitionInfoService;

    @Autowired
    private SoldierBasicInfoService soldierBasicInfoService;


    @Override
    public BasePagingResponse<List<SoldierPetitionInfoDTO>> searchSoldierPetitionInfo(final SoldierPetitionInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<SoldierPetitionInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页SoldierPetitionInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierPetitionInfoBSearcher soldierPetitionInfoBSearcher = SoldierPetitionInfoProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<SoldierPetitionInfoDO> soldierPetitionInfoDOList = soldierPetitionInfoService.search(soldierPetitionInfoBSearcher);
                final List<SoldierPetitionInfoDTO> soldierPetitionInfoDTOList = SoldierPetitionInfoProviderConverter.convertResponseDOList2DTOList(soldierPetitionInfoDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, soldierPetitionInfoDTOList, searchRequest, soldierPetitionInfoBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_PETITION_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<SoldierPetitionInfoDTO> getSoldierPetitionInfo(final BaseGetRequest baseGetRequest) {

        final BaseResponse<SoldierPetitionInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条SoldierPetitionInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final SoldierPetitionInfoDO soldierPetitionInfoDO = soldierPetitionInfoService.get((String) baseGetRequest.getId());
                final SoldierPetitionInfoDTO remoteObj = SoldierPetitionInfoProviderConverter.convertResponseDO2DTO(soldierPetitionInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_PETITION_INFO_GET;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<List<SoldierPetitionInfoDTO>> getSoldierPetitionInfoByIdCardNo(String idCardNo) {


        List<SoldierPetitionInfoDO> soldierPetitionInfoDOS = soldierPetitionInfoService.listByIdCardNo(idCardNo);
        final List<SoldierPetitionInfoDTO> soldierPetitionInfoDTOList = SoldierPetitionInfoProviderConverter.convertResponseDOList2DTOList(soldierPetitionInfoDOS);

        return ResponseUtils.buildSuccessResponse(soldierPetitionInfoDTOList);
    }


    @Override
    public BaseResponse<String> insertSoldierPetitionInfo(final SoldierPetitionInfoInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增SoldierPetitionInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final SoldierPetitionInfoDO soldierPetitionInfoDO = SoldierPetitionInfoProviderConverter.convertRequestIO2DO(insertRequest);
                SoldierBasicInfoDO byIdCardNo = soldierBasicInfoService.getByIdCardNo(insertRequest.getIdCardNo());
                if (byIdCardNo != null) {
                    soldierPetitionInfoDO.setAdminAreaCode(byIdCardNo.getResidentAdminAreaCode());
                    soldierPetitionInfoDO.setGender(byIdCardNo.getGender());
                    soldierPetitionInfoDO.setMobile(byIdCardNo.getMobile());
                }

                soldierPetitionInfoService.insert(soldierPetitionInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, soldierPetitionInfoDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_PETITION_INFO_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> batchInsert(List<SoldierPetitionInfoInsertRequest> requests) {

        if (CollectionUtils.isEmpty(requests)) {
            return ResponseUtils.buildSuccessResponse("0");
        }

        List<String> list = soldierBasicInfoService.listExistedIdCardNo(requests.stream().map(s -> s.getIdCardNo()).collect(Collectors.toSet()));
        if (CollectionUtils.isEmpty(list)) {
            return ResponseUtils.buildSuccessResponse("0");
        }
        int count = 1;
        for (SoldierPetitionInfoInsertRequest request : requests) {
            if (list.contains(request.getIdCardNo())) {
                SoldierPetitionInfoDO ss = soldierPetitionInfoService.getByStoreId(request.getStoreid());
                if (ss == null) {
                    final SoldierPetitionInfoDO soldierPetitionInfoDO = SoldierPetitionInfoProviderConverter.convertRequestIO2DO(request);
                    soldierPetitionInfoService.insert(soldierPetitionInfoDO);
                    count++;
                }
            }
        }
        return ResponseUtils.buildSuccessResponse(String.valueOf(count));
    }

    @Override
    public BaseResponse<String> batchInsertXfData(List<XfInsertRequest> requests) {

        return ResponseUtils.buildSuccessResponse(String.valueOf(0));
    }


    @Override
    public BaseResponse<Integer> updateSoldierPetitionInfo(final SoldierPetitionInfoUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改SoldierPetitionInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                SoldierPetitionInfoDO soldierPetitionInfoDO = SoldierPetitionInfoProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = soldierPetitionInfoService.update(soldierPetitionInfoDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_PETITION_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteSoldierPetitionInfo(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除SoldierPetitionInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
            }

            @Override
            public void executeService() {
                final Integer affectRowNum = soldierPetitionInfoService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.SOLDIER_PETITION_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }




}
